import pymysql
from pyecharts.charts import Pie
from pyecharts import options as opts

# 连接数据库
db = pymysql.connect(
    host='localhost',
    user='root',
    password='hhp20040512',
    database='crawlerDemo',
    charset='utf8'
)
cursor = db.cursor()

# SQL查询
sql = "select job_place,count(job_id) As num from job group by job_place"

try:
    cursor.execute(sql)
    res = cursor.fetchall()
    pie = Pie()
    pie.add("单位/个",res)

except:
    print("查询出问题了~")
finally:
    cursor.close()
    db.close()
    pie.set_global_opts(title_opts=opts.TitleOpts(title="岗位数量占比图"))
    # 渲染饼图到HTML文件
    pie.render("piera.html")


